home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: using const for structs?
- Date: 18 Jan 1996 19:01:11 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan18140111@g7240065.bridge.bst.bls.com>
- References: <4dm36c$c1h@zdi.informatik.uni-stuttgart.de>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Gerhard Muth's message of 18 Jan 1996 18:23:08 GMT
-
- In article <4dm36c$c1h@zdi.informatik.uni-stuttgart.de> Gerhard Muth <muth> writes:
-
- :// If i try to compile it on a HP 700 with
- :// $ CC const_struct.C
- :// i get the following error message:
- :// CC: "const_struct.C", line 35: error: cannot make a box (1284)
- ://
- :// I think, that i have to write a 2-parameter constructor,
- :// but the book does not tell anything about that. It just says:
- ://
- :// "The #define directive is limited to defining simple
- :// constants. The const statement can define almost any
- :// type of C++ constant including things such as
- :// structure classes."
- ://
- :// Question: Is the book wrong or is the compiler just bad?
-
- : struct box {
- float width, heigth; // float was int before
- : };
-
- : const box blue_box();
-
- I don't think this line does what you expected - this declares
- a function that returns a const box and takes no parameters.
- I believe you meant:
-
- const box blue_box;
-
- : const box pink_box(1.0, 4.5); // this is line 35
-
- This is a constructor call which you have not provided for struct
- box. The book is not wrong and compiler is not bad. You can declare
- initialise this struct like:
-
- const box pink_box = { 1.0, 4.5 };
-
- or you can define a constructor for struct box:
-
- struct box {
- box(float w = 0.0, float h = 0.0) : width(w), height(h)
- { }
-
- float width, heigth; // float was int before
- };
-
- now this works:
-
- const box pink_box(1.0, 4.5);
-
- and so does:
-
- const box blue_box;
-
- and:
-
- const box green_box(4.0);
-
- : void main(void) {}
-
- This has undefined behaviour - main should be declared
-
- int main(void)
- or
- int main(int argc, char* argv[])
-
- Hope this helps
- Regards
- -A.
- --
- | A.Champion |
-